package com.applet.mallappletbackstage.service.impl;

import com.applet.mallappletbackstage.entity.PersonnelInfo;
import com.applet.mallappletbackstage.entity.User;
import com.applet.mallappletbackstage.mapper.PersonnelInfoMapper;
import com.applet.mallappletbackstage.service.PersonnelInfoService;
import com.applet.mallappletbackstage.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X
 * @since 2020-08-17
 */
@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<PersonnelInfoMapper, PersonnelInfo> implements PersonnelInfoService {

    @Autowired
    private UserService userService;

    /***
     * 添加人员信息
     * @param openid
     * @param personnelInfo
     * @return
     */
    @Override
    public int addInfo(String openid, PersonnelInfo personnelInfo) {
        //查询用户信息
        User user = userService.getInfo(openid);
        String userId = user.getId();
        String proficiency = this.proficiency(Integer.valueOf(personnelInfo.getProficiency()));
        String composition = this.composition(Integer.valueOf(personnelInfo.getComposition()));
        personnelInfo.setOpenId(openid);
        personnelInfo.setUserId(userId);
        personnelInfo.setProficiency(proficiency);
        personnelInfo.setComposition(composition);
        return baseMapper.insert(personnelInfo);
    }

    /***
     * 查询数据库该用户数据
     * @param openid
     * @return
     */
    @Override
    public PersonnelInfo getInfo(String openid) {
        QueryWrapper<PersonnelInfo> personnelInfoQueryWrapper = new QueryWrapper<>();
        personnelInfoQueryWrapper.eq("open_id",openid);
        PersonnelInfo personnelInfo = baseMapper.selectOne(personnelInfoQueryWrapper);
        return personnelInfo;
    }

    /***
     * 更新该用户数据
     * @param openid
     * @param personnelInfo
     * @return
     */
    @Override
    public int upDataInfo(String openid, PersonnelInfo personnelInfo) {
        QueryWrapper<PersonnelInfo> personnelInfoQueryWrapper = new QueryWrapper<>();
        personnelInfoQueryWrapper.eq("open_id",openid);
        String proficiency = this.proficiency(Integer.valueOf(personnelInfo.getProficiency()));
        String composition = this.composition(Integer.valueOf(personnelInfo.getComposition()));
        personnelInfo.setProficiency(proficiency);
        personnelInfo.setComposition(composition);
        return baseMapper.update(personnelInfo,personnelInfoQueryWrapper);
    }

    private String proficiency(int count){
        String proficiency = "";
        switch (count) {
            case 1:
                proficiency = "学徒工（小工）";
                break;
            case 2:
                proficiency = "中级工（中工）";
                break;
            case 3:
                proficiency = "高级工（大工）";
                break;
            default:
                break;
        }
        return proficiency;
    }

    private String composition(int count){
        String proficiency = "";
        switch (count) {
            case 1:
                proficiency = "个人";
                break;
            case 2:
                proficiency = "班组";
                break;
            case 3:
                proficiency = "施工队";
                break;
            case 4:
                proficiency = "突击队";
                break;
            default:
                break;
        }
        return proficiency;
    }
}
